#!/bin/sh

E=/bin/echo

error() {
	v=1
	[ "$2" ] && v=$1 && shift
	echo $*
	exit $v
}

[ "$1" ] || error 1 "Syntax: $0 [tag] <testfile WITHOUT ext>"
[ "$2" ] && T=$1 && shift || T=`basename $1`
F=$1
DELAY=10

D=results-$T
mkdir $D || error 2 "Can't create directory '$D' ... aborting"

$E -n "Checking R original file $F: "
[ -f "$F.R" ] || error 3 "file 'doen't exists"
$E OK

$E -n "Checking R bcode file $F.Rbc: "
[ -f "$F.Rbc" ] || error 4 "compiler::cmpfile(\"$F.R\", out=\"$F.Rbc\")" | R --quiet --vanilla || exit 5 "Can't compile"
$E OK

$E -n "Checking Reactor bcode file $F.Rc: "
[ -f "$F.Rc" ] || $REACTOR_PATH/Release/bin/rbcc $F.R
$E OK

TAG="R $TAG" ./benchmark.sh --save $D/R R --vanilla --slave -f $F.R
sleep $DELAY
TAG="Rbc $TAG" ./benchmark.sh --save $D/Rbc R --vanilla -f test_compile.R --slave --args $F.Rbc
sleep $DELAY
TAG="Reactor $TAG" ./benchmark.sh --save $D/Reactor $REACTOR_PATH/Release/bin/reactor $F.Rc

R --slave -f benchmark.R --args $D/*
